package com.ygou.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygou.mapper.UserAddressMapper;
import com.ygou.pojo.UserAddress;
import com.ygou.pojo.UserAddressExample;
import com.ygou.pojo.UserAddressExample.Criteria;
import com.ygou.service.UserAddressService;

@Service
public class UserAddressServiceImpl implements UserAddressService {

	@Autowired
	private UserAddressMapper userAddressMapper;
	
	@Override
	public List<UserAddress> findAddressByUId(Integer userid) {
		UserAddressExample example=new UserAddressExample();
		Criteria c=example.createCriteria();
		c.andUseridEqualTo(userid);
		return userAddressMapper.selectByExample(example);
	}
	
	@Override
	public boolean insert(UserAddress record) {
		UserAddressExample example=new UserAddressExample();
		Criteria c=example.createCriteria();
		c.andUseridEqualTo(record.getUserid());
		c.andIsdefaultEqualTo((byte)0);
		List<UserAddress> address=userAddressMapper.selectByExample(example);
		if(address==null || address.size()==0){
			record.setIsdefault((byte)0);
		}else{
			record.setIsdefault((byte)1);
		}
		record.setCreatetime(new java.util.Date());
		return userAddressMapper.insert(record)>0;
	}

	@Override
	public UserAddress selectByPrimaryKey(Integer addressid) {
		return userAddressMapper.selectByPrimaryKey(addressid);
	}

	@Override
	public boolean updateByPrimaryKey(UserAddress record) {
		return userAddressMapper.updateByPrimaryKey(record)>0;
	}

	@Override
	public boolean deleteByPrimaryKey(Integer addressid) {
		return userAddressMapper.deleteByPrimaryKey(addressid)>0;
	}

	@Override
	public boolean updateByPrimaryKeySelective(UserAddress record) {
		return userAddressMapper.updateByPrimaryKeySelective(record)>0;
	}

	@Override
	public boolean updDefaultArs(Integer addressid) {
		userAddressMapper.updateAll();
		UserAddress address=new UserAddress();
		address.setIsdefault((byte)0);
		address.setAddressid(addressid);
		return userAddressMapper.updateByPrimaryKeySelective(address)>0;
	}

}
